﻿//界面内容类型变量和常量
var YEASTERDAY=1;
var MONTH=2;
var vPageType=YEASTERDAY;
// 渠道和版本常量；
var CHANNEL="1";
var VERSION="2";
var vChannelCode=CHANNEL;
//月份查寻的时间值；
var vQueryTime="";

/*****************************************************/
function isYesterday(){
	return (vPageType==YEASTERDAY)?true:false;
}

window.onload= function(){	
	 var thisURL = document.URL; 
    console.log("thisURL="+thisURL);	
	vPageType=getOptType(thisURL);	 
	console.log("vPageType="+vPageType);
	setTitleBar(vPageType);
	if(isYesterday()){
		requestYesterDayNewUsers(CHANNEL);
	}else{
		var month="201607";
		var date= new Date();
		var y= date.getFullYear();
		var m=date.getMonth()+1;
		if(m<10){
			m="0"+m;			
		}		
		vQueryTime=y+m;
		console.log(vQueryTime);
		requestNewUsersMonthly(vQueryTime,CHANNEL);
	}	
}
function setTitleBar(vPageType){	
	var btnTitle = document.getElementById("buttonTitle");
	if(isYesterday()){
		strTime=getYesterdayTip()+" 新增用户";
		setTimetip(strTime);
	}else{
		//title.innerHTML="本月累计新增用户";
		setTitle("本月累计新增用户");
		//buttonTitle.className="input-button-style2";
		strTime=getLastMouthTip()+" 新增用户";
		setTimetip(strTime);
		buttonTitle.value="选择月份";
	}
}

function setTitle(title){
	var titleElement= document.getElementById("spanTitle");
	titleElement.innerHTML=title;
}

function setTimetip(strTime){
	var tTimetip=document.getElementById("timetip");
	tTimetip.innerHTML=strTime;
}

function getOptType(thisUrl){
	  var  value =thisUrl.split('?')[1];  
	  var  optType= value.split('=')[1];  	
	  return optType;
}

function on15Days(){
	if(isYesterday()){
		window.location.href="NewUser15Days.html";
	}else{		
		showMonthDialog();
	}
	
}
function onSelect(e){
	cleanWhitespace(e.parentNode);
	e.className="select-style";
	var sibling =e.previousSibling;	
	if(sibling==null){
		sibling=e.nextSibling;
		vChannelCode=CHANNEL;
		if(isYesterday()){
			requestYesterDayNewUsers(vChannelCode);		
		}else{
		   var month="201607";
			requestNewUsersMonthly(month,vChannelCode);
		}		
	}else{
		vChannelCode=VERSION;
		if(isYesterday()){
			requestYesterDayNewUsers(vChannelCode);	
		}else{
			var month="201607";
			requestNewUsersMonthly(month,vChannelCode);
		}			
	}
	
	sibling.className="unselect-style";
	var title=e.innerHTML;
	console.log("title:"+title);
	var tHeadTitle= document.getElementById("thead-left");
	tHeadTitle.innerHTML=title;	
}

function cleanWhitespace(oEelement){
	for(var i=0;i<oEelement.childNodes.length;i++){
	var node=oEelement.childNodes[i];
		if(node.nodeType==3 && !/\S/.test(node.nodeValue)){
		node.parentNode.removeChild(node)}
	}
}


function getYesterday(){
	var today = new Date();	
    var y= today.getTime()-24*60*60*1000;
	var yester= new Date();
	yester.setTime(y);
	var year= yester.getFullYear();
	var month= yester.getMonth()+1;
	var day= yester.getDate();	
    month= month<10?('0'+month):(''+month);
	day= day<10?('0'+day):(''+day);
	var result= year+month+day;
	return result;
}

function getYesterdayTip(){
	var today = new Date();	
    var y= today.getTime()-24*60*60*1000;
	var yester= new Date();
	yester.setTime(y);
	var year= yester.getFullYear();
	var month= yester.getMonth()+1;
	var day= yester.getDate();	
    month= month<10?('0'+month):(''+month);
	day= day<10?('0'+day):(''+day);
	var result= year+"."+month+"."+day;
	return result;
}


function getLastMouthTip(){
	var today = new Date();	
    var y= today.getTime()-24*60*60*1000;
	var yester= new Date();
	yester.setTime(y);
	var year= yester.getFullYear();
	var month= yester.getMonth()+1;
	var day= yester.getDate();	
    month= month<10?('0'+month):(''+month);
	day= day<10?('0'+day):(''+day);
	var result= year+"."+month;
	return result;
}

function setTableContent(strContent){
	var tbody= document.getElementById('tbodyContent');
	tbody.innerHTML=strContent;
}
/*****************************************
昨日累计新用户的数据请求及处理
*******************************************/
function requestYesterDayNewUsers(channelCode){
	var optType='1';
	if(!UIDebug){
		var queryTime=getYesterday();	
	}else{
		var queryTime='20160719';
	}
	var requestUrl =getNewUserUrl(optType, channelCode, queryTime);
	console.log(' url= '+requestUrl);
	$.ajax({
		type:"post",
		url:requestUrl,
		success:function(resultData){
			processData(channelCode,resultData);		
		}
	});
}
function processData(channelCode,resultData){
	console.log("processData--------channelCode="+channelCode);		
	var flag = resultData.success;	
	console.log("flag="+flag);		
	if(flag==1){
		var vData=resultData.data;
		console.log("vData="+vData+","+vData.length);	
		var strContent="";
		var chartDataArray= new Array();
		if(channelCode==CHANNEL){
			for(var i=0;i<vData.length;i++){
				var users= Math.floor(vData[i].USERNUM/10000);						
				var userNum= users+'w';
				chartDataArray[i]=[vData[i].CHNLNAME,users];		
				strContent+="<tr><td>"+vData[i].CHNLNAME+"</td><td>"+userNum+"</td></tr>";
			}	
		}else if(channelCode==VERSION){
			for(var i=0;i<vData.length;i++){				
				var users= Math.floor(vData[i].USERNUM/10000);		
                chartDataArray[i]=[vData[i].CLENTVERSION,users];		
				var userNum= users+'w';					
				strContent+="<tr><td>"+vData[i].CLENTVERSION+"</td><td>"+userNum+"</td></tr>";
			}	
		}
		console.log("strContent="+strContent);
		setTableContent(strContent);
		drawChart(chartDataArray);
	}
}
function drawChart(dataArray){
	var colors=new Array();
	for(var i=0;i<dataArray.length;i++){
		console.log(dataArray[i]);
		colors[i] = '#ff0000';
	}
	
	var myChart = new JSChart('chart', 'bar');	
	myChart.setDataArray(dataArray);	
	myChart.colorizeBars(colors);
	myChart.setTitle('');
	myChart.setAxisNameX('');
	myChart.setAxisNameY('');	
	myChart.setAxisPaddingTop(0);
	myChart.setAxisPaddingLeft(20);	
	myChart.setTextPaddingBottom(8);	
	myChart.setBarSpacingRatio(50);
	myChart.resize(screen.width, 197);
	myChart.setGridColor('#5D5F5D');
	myChart.setGrid(true);
	myChart.draw();
}
/**********************************************************
月累计新用户的数据请求及处理
***********************************************************/
function showMonthDialog(){
	console.log("----showMonthDialog-------");	
	document.getElementById("monthSelect").style.display='block';
	document.getElementById("monthSelect").style.opacity=1;  
    var months=get12Months();
	var strHtml="";
    for(var i=0;i<months.length;i++){
		strHtml+="<li onclick=\"onMonthSelect(this)\">"+months[i]+"月</li>";
	}
	console.log(" strHtml--"+strHtml);
	var ulElement= document.getElementById("monthList");
	ulElement.innerHTML=strHtml;
	pubFunciton.moveStop();
}

function  onMonthSelect(e){	
	dismissMonthDialog();
	var content= e.innerHTML;
	pubFunciton.moveStart();
	setTitle(content+"累计新增用户");
	content=content.substring(0,content.length-1);
	console.log("-content--"+content);
	var date= new Date();
	var curMonth= date.getMonth()+1;
	var curYear=date.getFullYear();
	var queryYear="";
	var queryMonth="";
	if(content>curMonth){
		queryYear=(curYear-1);		
	}else{
		queryYear=curYear;	
	}	
	if(content<10){
		queryMonth="0"+content;		
	}else{
		queryMonth=content;	
	}
	vQueryTime=queryYear+queryMonth;
	console.log(vQueryTime);
	requestNewUsersMonthly(vQueryTime,vChannelCode);
}

function dismissMonthDialog(){
	console.log("----dismissMonthDialog-------");
	document.getElementById("monthSelect").style.display='none';
	document.getElementById("monthSelect").style.opacity=0; 
}

function requestNewUsersMonthly(month,channelCode){
	var optType='2';	
	var requestUrl=getNewUserUrl(optType, channelCode, month);
	console.log("requestUrl="+requestUrl);
	var procesMonthData= function (resultData){
		processData(channelCode,resultData);		
	}
	sendRequest(requestUrl,procesMonthData);
}

function get12Months(){
	var today= new Date();
	var thisMonth= today.getMonth();	
	var months= new Array();
	var t= 12-(thisMonth+1);
	var i=0;
	for(;i<12&&thisMonth>=0;i++,thisMonth--){
		months[i]=thisMonth+1;
	}	
	for(var j=0;j<t;j++,i++){
		months[i]=12-j;
	}	
	console.log(months);
	return months;
}

var pubFunciton = {
	moveStop:function(){//停止滚动
		window.ontouchmove=function(e){
			e.preventDefault && e.preventDefault();
			e.returnValue=false;
			e.stopPropagation && e.stopPropagation();
			return false;
		}
	},
	moveStart:function(){//可以滚动
		window.ontouchmove = "";
	}  
} 

